package com.quanshu.shop.goods.view;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.gui.login.LoginActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.goods.adapter.SearchGoodsListAdapter;
import com.quanshu.shop.goods.entity.SearchListEntity;
import com.quanshu.shop.main.api.ShopMainIF;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchGoodsListActivity extends BaseActivity {

    @BindView(R.id.goods_list_title)
    BaseTitleBar goodsListTitle;
    @BindView(R.id.goods_list_rl)
    RecyclerView goodsListRl;
    private String mTitle = null;
    private SearchGoodsListAdapter searchGoodsListAdapter;
    private List<SearchListEntity> dataEntityList;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_goods_list);
        mContext=this;
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).titleBar(goodsListTitle).statusBarDarkFont(true).init();
        Bundle bundle = getIntent().getExtras();
        mTitle = bundle.getString(SearchActivity.SEARCH_TXT, "未知");
        goodsListTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText(mTitle).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        //GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        goodsListRl.setLayoutManager(staggeredGridLayoutManager);
    }

    private void initData() {
        NetClient.getInstance().getShopService(ShopMainIF.class)
                .getSearchGoodsListInfo(mTitle)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<SearchListEntity>>>() {
                    @Override
                    public void onFailure(Throwable e) {
                        ToastHelper.toast("暂未搜到商品");
                    }

                    @Override
                    public void onSuccess(BaseResponse<List<SearchListEntity>> response) {
                        dataEntityList = response.getData();
                        searchGoodsListAdapter = new SearchGoodsListAdapter(dataEntityList, mContext);
                        searchGoodsListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                switch (view.getId()) {
                                    case R.id.goods_list_cl:
                                        if (MyApplication.getInstance().getCurrentUser() == null) {
                                            BaseTool.goActivity(mContext, LoginActivity.class, null);
                                            return;
                                        }
                                        Bundle bundle = new Bundle();
                                        bundle.putString("goods_info_id", dataEntityList.get(position).getId());
                                        BaseTool.goActivity(mContext, GoodsInfoActivity.class, bundle);
                                        break;
                                }
                            }
                        });
                        goodsListRl.setAdapter(searchGoodsListAdapter);
                    }
                });
    }
}
